#include "head.h"

int main(int argc, char* argv[])
{
    //初始化公共部分
    statu_t status;
    init_status(&status,4);

    //启动子线程要将公共的部分传给子线程
    init_pthread(&status);

    //创建三次握手的socket
    int socket_fd;
    init_socket(&socket_fd,"8080","192.168.44.128");

    //设置epoll监听有没有新的连接进来
    int epoll_fd = epoll_create(1);
    add_epoll(epoll_fd,socket_fd);

    while(1){
        struct epoll_event events[2];
        memset(events,0,sizeof(events));

        int epoll_num = epoll_wait(epoll_fd,events,2,-1);

        for(int i=0;i<epoll_num;i++){
            int fd = events[i].data.fd;
            if(fd == socket_fd){
                int net_fd = accept(socket_fd,NULL,NULL);

                pthread_mutex_lock(&status.lock);
                enQueue(&status.pQueue,net_fd);
                
                //唤醒所有的子线程
                pthread_cond_broadcast(&status.cond);

                pthread_mutex_unlock(&status.lock);
            }
        }
    }
    return 0;
}

